<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/27
 * Time: 15:52
 */

namespace App\Services;


class Filter
{
    /**
     * 过滤敏感词
     * @param string $text
     * @return string
     */
    public static function init($text = '')
    {
        if (!$text) {
            return $text;
        }

        $filter_files = config('app.filter_files');
        $db_config = config('database.connections.mysql');

        foreach ($filter_files as $files) {
            $command = 'python ' . base_path($files) . " \"{$text}\" \"{$db_config['host']}\" \"{$db_config['username']}\" \"{$db_config['password']}\" \"{$db_config['database']}\"";
            exec($command, $output);
            if (count($output) > 0) {
                $output = json_decode($output[0], true);
                $err_code = json_last_error();
                if ($err_code !== JSON_ERROR_NONE) {
                    $text = $output['text'];
                }
            }
        }

        return $text;
    }
}